package fragmentson;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.wq.cycling.DemoApplication;
import com.wq.cycling.R;

import bean.MessageForSimple;
import fragmentson.backhandle.BackHandledFragment;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import service.ApiUtil;
import util.myview.SwitchView;

/**
 * 警告开关
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */
public class SwitchMenu extends BackHandledFragment {

	private View rootView;
	private SwitchView switchView1,switchView2,switchView3,switchView4,switchView5,switchView6,switchView7,switchView8,switchView9;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
							 Bundle savedInstanceState) {
		rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_switchmenu, null);
		initData();
		initView();
		return rootView;
	}

	private void initData(){
		ApiUtil.getApiService().getsetting(DemoApplication.getToken(),DemoApplication.getDeviceid()).enqueue(new Callback<MessageForSimple>() {
			@Override
			public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
				try {
					MessageForSimple entity = response.body();
					if(entity.getCode().equals("10008")){
						switchView1.setOpened(entity.getData().isOne());
						switchView2.setOpened(entity.getData().isTwo());
						switchView3.setOpened(entity.getData().isThree());
						switchView4.setOpened(entity.getData().isFour());
						switchView5.setOpened(entity.getData().isFive());
						switchView6.setOpened(entity.getData().isSix());
						switchView7.setOpened(entity.getData().isSeven());
						switchView8.setOpened(entity.getData().isEight());
						switchView9.setOpened(entity.getData().isNine());
					}else {
						Toast.makeText(getActivity(), entity.getMsg(), Toast.LENGTH_LONG).show();
					}
				}catch (Exception e){
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(Call<MessageForSimple> call, Throwable t) {
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) rootView.findViewById(R.id.tv_title)).setText(R.string.sos_type_setting);
		switchView1 = ((SwitchView) rootView.findViewById(R.id.switchview1));
		switchView2 = ((SwitchView) rootView.findViewById(R.id.switchview2));
		switchView3 = ((SwitchView) rootView.findViewById(R.id.switchview3));
		switchView4 = ((SwitchView) rootView.findViewById(R.id.switchview4));
		switchView5 = ((SwitchView) rootView.findViewById(R.id.switchview5));
		switchView6 = ((SwitchView) rootView.findViewById(R.id.switchview6));
		switchView7 = ((SwitchView) rootView.findViewById(R.id.switchview7));
		switchView8 = ((SwitchView) rootView.findViewById(R.id.switchview8));
		switchView9 = ((SwitchView) rootView.findViewById(R.id.switchview9));

		switchView1.setChangeListener(listener);
		switchView2.setChangeListener(listener);
		switchView3.setChangeListener(listener);
		switchView4.setChangeListener(listener);
		switchView5.setChangeListener(listener);
		switchView6.setChangeListener(listener);
		switchView7.setChangeListener(listener);
		switchView8.setChangeListener(listener);
		switchView9.setChangeListener(listener);

	}

	public static SwitchMenu newInstance() {
		SwitchMenu fragment = new SwitchMenu();
		return fragment;
	}

	public SwitchView.ChangeListener listener = new SwitchView.ChangeListener() {
		@Override
		public void listener(View v, boolean ischecked) {
			switch (v.getId()){
				case R.id.switchview1:
					submit(1,ischecked);
					break;
				case R.id.switchview2:
					submit(2,ischecked);
					break;
				case R.id.switchview3:
					submit(3,ischecked);
					break;
				case R.id.switchview4:
					submit(4,ischecked);
					break;
				case R.id.switchview5:
					submit(5,ischecked);
					break;
				case R.id.switchview6:
					submit(6,ischecked);
					break;
				case R.id.switchview7:
					submit(7,ischecked);
					break;
				case R.id.switchview8:
					submit(8,ischecked);
					break;
				case R.id.switchview9:
					submit(9,ischecked);
					break;
				default:
					break;
			}
		}
	};
	public void submit(final int index,final boolean ischecked){
		ApiUtil.getApiService().setting(DemoApplication.getToken(),DemoApplication.getDeviceid(),index+"",ischecked+"").enqueue(new Callback<MessageForSimple>() {
			@Override
			public void onResponse(Call<MessageForSimple> call, Response<MessageForSimple> response) {
				try {
					MessageForSimple entity = response.body();
					if(entity.getCode().equals("10008")){
					}else{
						Toast.makeText(getActivity(),entity.getMsg(),Toast.LENGTH_LONG).show();
						switch (index){
							case 1:
								switchView1.setOpened(!ischecked);
								break;
							case 2:
								switchView2.setOpened(!ischecked);
								break;
							case 3:
								switchView3.setOpened(!ischecked);
								break;
							case 4:
								switchView4.setOpened(!ischecked);
								break;
							case 5:
								switchView5.setOpened(!ischecked);
								break;
							case 6:
								switchView6.setOpened(!ischecked);
								break;
							case 7:
								switchView7.setOpened(!ischecked);
								break;
							case 8:
								switchView8.setOpened(!ischecked);
								break;
							case 9:
								switchView9.setOpened(!ischecked);
								break;
							default:
								break;
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					switch (index){
						case 1:
							switchView1.setOpened(!ischecked);
							break;
						case 2:
							switchView2.setOpened(!ischecked);
							break;
						case 3:
							switchView3.setOpened(!ischecked);
							break;
						case 4:
							switchView4.setOpened(!ischecked);
							break;
						case 5:
							switchView5.setOpened(!ischecked);
							break;
						case 6:
							switchView6.setOpened(!ischecked);
							break;
						case 7:
							switchView7.setOpened(!ischecked);
							break;
						case 8:
							switchView8.setOpened(!ischecked);
							break;
						case 9:
							switchView9.setOpened(!ischecked);
							break;
						default:
							break;
					}
				}
			}
			@Override
			public void onFailure(Call<MessageForSimple> call, Throwable t) {
				Toast.makeText(getActivity(),getResources().getString(R.string.error),Toast.LENGTH_LONG).show();
			}
		});
	}

	@Override
	public boolean onBackPressed() {
		return false;
	}

}
